﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Weight.Model;
using Dapper;
using System.Data;
using Weight.Base;
using Dapper.Contrib.Extensions;

namespace Weight.Dal
{
   public  class DalPhrase
    {
        /// <summary>
        /// 查询所有词组
        /// </summary>
        /// <param name="category">类别</param>
        /// <param name="name">名称</param>
        /// <returns></returns>
        public  IEnumerable<PhraseEnity> GetPhrases(string category, string nameOrPinYin,string extFilter)
        {
            using(IDbConnection conn = SessionFactory.CreateConnection())
            {
                string sql = " select * from Phrase  where  Category=@Category";
                if(!string.IsNullOrEmpty(nameOrPinYin))
                {
                    nameOrPinYin = "%" + nameOrPinYin + "%";
                    sql += " and (Name like @nameOrPinYin or PinYin like @nameOrPinYin)";
                }

                if(!string.IsNullOrEmpty(extFilter))
                {
                    sql += " and " + extFilter;
                }

                return conn.Query<PhraseEnity>(sql,new {category, nameOrPinYin });
            }
        }

        /// <summary>
        /// 获取所有的词组
        /// </summary>
        /// <returns></returns>
        public IEnumerable<PhraseEnity> GetAllPhrases()
        {
            using (IDbConnection conn = SessionFactory.CreateConnection())
            {
                string sql = " select * from Phrase ";
                return conn.Query<PhraseEnity>(sql);
            }
        }

        /// <summary>
        /// 获取词组通过Id
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns></returns>
        public PhraseEnity GetPhraseById(string id)
        {
            using (IDbConnection conn = SessionFactory.CreateConnection())
            {
                string sql = "select * from Phrase where Id=@id";
                return conn.Query<PhraseEnity>(sql,new { id}).FirstOrDefault();
            }
        }

        /// <summary>
        ///添加词组
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public long Add(PhraseEnity entity)
        {
            using(IDbConnection conn =SessionFactory.CreateConnection())
            {
               return  conn.Insert(entity);
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Updae(PhraseEnity entity)
        {
            using(IDbConnection conn  = SessionFactory.CreateConnection())
            {
                return conn.Update(entity);
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns></returns>
        public bool Del(string id)
        {
            using (IDbConnection conn = SessionFactory.CreateConnection())
            {
                string sql = "delete from Phrase where Id=@id";
                return conn.Execute(sql, new { id }) > 0 ? true : false;
            }
        }
       
    }
}
